import re
import java.lang
from org.apache.hadoop.hbase import HBaseConfiguration, HTableDescriptor, HColumnDescriptor, HConstants
from org.apache.hadoop.hbase.client import HBaseAdmin, HTable, Put, Get

conf = HBaseConfiguration.create()
admin = HBaseAdmin(conf)

def process (parse_result_dict):
	'''
	check the existence of table and family in:
	1) select list
	2) table list
	3) conditon list
	'''
	check_result = True
	check_result &= check_select_list(parse_result_dict['select_list'])
	check_result &= check_table_list(parse_result_dict['table_list'])
	check_result &= check_condition_list(parse_result_dict['condition_list'])
	return check_result


def check_select_list(select_list):
	'''
		1. check format		talbe.family:qualifier
		2. check existense of table and family

		return True of False
	'''
	print 'check_select_list %s' % select_list
	for table_value in select_list:
		print 'for %s ' % table_value
		ret = parse_table_value(table_value)
		if ret == None:
			return False
		(table, family, qualifier) = ret	
		try:
			print 'checking table value: %s, %s' %(table, family)
			check_table_value(table, family)
		except Exception:
			print 'check_table_value() error'
			return False
	return True

def check_table_list (table_list):
	'''
		check list whose format is #[table1, table2, [table_value11, table_value21], table3, [table_value21, table_value22]..]
		return True or False
	'''
	list_len = len(table_list)
	if list_len % 2 != 1 :
		print 'table list syntax error, incorrect list length'
		return False
	if not table_exists(table_list[0]):
		print 'table %s does not exist' % table_list[0]
		return False
	idx = 1
	while idx + 2 <= list_len :
		table = table_list[idx]
		if not table_exists(table):
			print 'table %s does not exist' % table
			return False
		table_value_list = table_list[idx+1]
		try:
			check_table_value_list(table_value_list)
		except Exception:
			return False
		idx = idx + 2
	return True

		
def check_condition_list(condition_list):
	if isinstance(condition_list, list):
		ret = True
		for inner_list in condition_list:
			ret &= check_condition_list(inner_list)
		return ret
	else:
		#parse string 'table.family:qualifer = xxx'
		table_value = condition_list.split(' ')[0]
		try:
			parse_check_table_value(table_value)
		except Exception:
			print 'parse and check %s error' % table_value
			return False
		return True


def check_table_value_list(table_value_list):
	for (table, family) in table_value_list:
		check_table_value(table, familly)


def parse_check_table_value(table_value):
	ret = parse_table_value(table_value)
	if ret == None:
		raise Exception
	(table, family, qualifier) = ret
	check_table_value(table, family)


def check_table_value(table, family):
	if table_exists(table) == False:
		print "table '%s' does not exist" % table
		raise Exception
	if family_exists(table, family) == False:
		print "family '%s' in table '%s' does not exist" % (family, table)
		raise Exception


def parse_table_value(table_value):
	'''
		parse table.family.qualifier
		return (table, family, qualifier) or None
	'''
	valid = re.compile(r'^([a-zA-Z0-9\-_]+)\.([a-zA-Z0-9\-_]+)\:([a-zA-Z0-9\-_]+)$')
	match_result = valid.match(table_value)
	if match_result == None:
		print 'table result does not match'
		return None
	return match_result.groups()
	

def table_exists (table_name):
	return admin.tableExists(table_name)

def table_list_exist (table_list):
	for table_name in table_list:
		if table_exists(table_name) is False:
			return False
	return True


def family_exists (table_name, family_name):
	if table_exists(table_name) is False:
		return False
	table = HTable(conf, table_name)
	table_desc = table.getTableDescriptor()
	
	return table_desc.hasFamily(family_name)


def family_collide (table_list, family_name):
	'''
		check whether 2 or more table in table_list have the same family_name
	'''
	check = False
	for table_name in table_list:
		if family_exists(table_name, family_name):
			if check == True:
				return True
			else:
				check = True
	return False

def find_table (table_list, family_name):
	'''
		find the 1st table with family_name
		return table name or None
	'''
	for table_name in table_list:
		if family_exists(table_name, family_name):
			return table_name
	return None


class PreProcessException(Exception):
	pass


if __name__ == '__main__':
	my_list = [
		['a = 1', 'b = 2'],
		'c = 3',
	]
	check_condition_list(my_list)
